﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AqueleAjax.UI;
using AqueleAjax.UI.Controls;

namespace AqueleAjax
{
    public partial class _Default : System.Web.UI.Page
    {
        private DataTable _dados;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.ddlCores.Items.Add("Selecione");
                this.ddlCores.Items.Add("Azul");
                this.ddlCores.Items.Add("Amarelo");
                this.ddlCores.Items.Add("Vermelho");
                this.ddlCores.Items.Add("Branco");

                DataTable tbl = new DataTable();
                DataRow novaLinha = null;

                tbl.Columns.Add("ID", typeof(int));
                tbl.Columns.Add("Nome", typeof(string));
                tbl.Columns.Add("Idade", typeof(int));

                novaLinha = tbl.NewRow();
                novaLinha["ID"] = "1";
                novaLinha["Nome"] = "teste teste";
                novaLinha["Idade"] = "19";
                tbl.Rows.Add(novaLinha);

                novaLinha = tbl.NewRow();
                novaLinha["ID"] = "2";
                novaLinha["Nome"] = "teste";
                novaLinha["Idade"] = "29";
                tbl.Rows.Add(novaLinha);

                novaLinha = tbl.NewRow();
                novaLinha["ID"] = "3";
                novaLinha["Nome"] = "teste teste teste";
                novaLinha["Idade"] = "9";
                tbl.Rows.Add(novaLinha);

                novaLinha = tbl.NewRow();
                novaLinha["ID"] = "4";
                novaLinha["Nome"] = "teste teste teste teste";
                novaLinha["Idade"] = "19";
                tbl.Rows.Add(novaLinha);

                this.grvTeste.DataSource = tbl;
                this.grvTeste.DataBind();
            }
        }

        private void ddlCores_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlSubCores.Items.Clear();

            if (this.ddlCores.SelectedIndex > 0)
            {
                this.ddlSubCores.Items.Add("Selecione");
                this.ddlSubCores.Items.Add("Sub Azul");
                this.ddlSubCores.Items.Add("Sub Amarelo");
                this.ddlSubCores.Items.Add("Sub Vermelho");
                this.ddlSubCores.Items.Add("Sub Branco");

                this.mainControl.ClientScript.RegisterScript("testeCor", string.Format("alert('Cor selecionada: {0}');", this.ddlCores.SelectedValue));
            }

            this.mainControl.Update(this.ddlSubCores);
        }

        private void ddlSubCores_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lblCor.Text = string.Empty;

            if (this.ddlSubCores.SelectedIndex > 0)
            {
                this.lblCor.Text = this.ddlSubCores.SelectedValue;
            }

            this.lblHora.Text = DateTime.Now.ToString();

            this.pnlLabelCores.Visible = true;

            this.mainControl.Update(this.pnlLabelCores);
        }

        protected void ddlGridCores_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((AjaxDropDownList)sender).NamingContainer;
            Label lblGridCor = (Label)row.FindControl("lblGridCor");

            lblGridCor.Text = ((AjaxDropDownList)sender).SelectedValue;

            row.BackColor = System.Drawing.Color.Aqua;

            this.mainControl.Update(this.grvTeste);
        } 

        private void btnTeste_Click(object sender, EventArgs e)
        {
            if (this.ddlCores.SelectedIndex > 0)
            {
                this.lblResumoCor.Text = this.ddlCores.SelectedValue;
            }

            if (this.ddlSubCores.SelectedIndex > 0)
            {
                this.lblResumoSubcor.Text = this.ddlSubCores.SelectedValue;
            }
        }

        private void txtAC_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtAC.Text))
            {
                this._dados.DefaultView.RowFilter = string.Format("Nome LIKE '%{0}%'", this.txtAC.Text);

                this.txtAC.Result.DataSource = this._dados.DefaultView;
            }
            else
            {
                this.txtAC.Result.DataSource = null;
            }
        }

        private void Result_ItemDataBound(object sender, AutoCompleteResultEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                AjaxLinkButton btnSelecionar = (AjaxLinkButton)e.Item.FindControl("btnSelecionar");
                System.Data.DataRowView row = (System.Data.DataRowView)e.Item.DataItem;

                btnSelecionar.Text = row["Nome"].ToString();
                btnSelecionar.CommandArgument = row["ID"].ToString();
                btnSelecionar.CommandName = "Testar";
            }
        }

        private void Result_ItemCommand(object sender, AutoCompleteResultCommandEventArgs e)
        {
            //this.Page.ClientScript.RegisterStartupScript(this.GetType(), "selecao", string.Format("alert('Autocomplete selecionado: {0}');", e.SelectedValue), true);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ddlCores.SelectedIndexChanged += new EventHandler(this.ddlCores_SelectedIndexChanged);
            this.ddlSubCores.SelectedIndexChanged += new EventHandler(this.ddlSubCores_SelectedIndexChanged);
            this.btnTeste.Click += new EventHandler(this.btnTeste_Click);
            this.txtAC.TextChanged += new EventHandler(this.txtAC_TextChanged);

            this.txtAC.Result.ItemDataBound += new AutoCompleteResultEventHandler(this.Result_ItemDataBound);
            this.txtAC.Result.ItemCommand += new AutoCompleteResultCommandEventHandler(this.Result_ItemCommand);

            this.AjaxLinkButton1.Click += new EventHandler(this.AjaxLinkButton1_Click);

            this.CarregarDados();
        }

        private void AjaxLinkButton1_Click(object sender, EventArgs e)
        {
            
        }

        private void CarregarDados()
        {
            this._dados = new DataTable();
            DataRow novaColuna = null;

            this._dados.Columns.Add(new DataColumn("ID", typeof(int)));
            this._dados.Columns.Add(new DataColumn("Nome", typeof(string)));

            string[] arquivos = System.IO.Directory.GetFiles("C:\\Windows");

            for (int i = 0; i < arquivos.Length; i++)
            {
                novaColuna = this._dados.NewRow();
                novaColuna["ID"] = Convert.ToString(i + 1);
                novaColuna["Nome"] = arquivos[i];
                this._dados.Rows.Add(novaColuna);
            }
        }
    }
}
